package com.hyg.oa.user.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hyg.oa.common.PageUtils;
import com.hyg.oa.common.Pagination;
import com.hyg.oa.common.UUIDUtils;
import com.hyg.oa.user.model.User;
import com.hyg.oa.user.service.UserService;

/**
 * Controller类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/user") 
public class UserController {
	
	@Autowired
	private UserService userService;
	
	/**
	 * 登陆
	 * @return
	 */
	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String,Object>();
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		User user = this.userService.findByUsername(username);
		if(user != null && user.getPassword().equals(password)) {
			
			HttpSession session =  request.getSession();
			session.setAttribute("username", username);
			
			return new ModelAndView("/jsp/index.jsp", model);
		} else {
			return new ModelAndView("/jsp/login.jsp", model);
		}
		
	}
	
	/**
	 * 退出
	 * @return
	 */
	@RequestMapping("/logout")
	public ModelAndView logout(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String,Object>();
		
		HttpSession session =  request.getSession();
		session.invalidate();
		
		return new ModelAndView("/jsp/login.jsp", model);
	}
	
	
	/**
	 * page
	 * @return
	 */
	@RequestMapping("/page")
	public ModelAndView page(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String,Object>();
		
		//总数
		long totalCount = this.userService.findTotalCount();
		//page对象
		Pagination pagination = PageUtils.create(request, totalCount);
		
		//数据
		List<User> list = this.userService.findPage(pagination.getPageNo(), pagination.getPageSize());
		model.put("list", list);
		model.put("pagination", pagination);
		
		System.out.println("user page");
		
		return new ModelAndView("/jsp/user/list.jsp", model);
	}
	
	/**
	 * 用户列表
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list() {
		Map<String, Object> model = new HashMap<String,Object>();
		
		List<User> list = this.userService.findAll();
		model.put("list", list);
		
		return new ModelAndView("/jsp/user/list.jsp", model);
	}
	
	/**
	 * 用户新增
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add() {
		return new ModelAndView("/jsp/user/add.jsp");
	}
	
	
	
	/**
	 * 保存
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Object save(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String realname = request.getParameter("realname");
			String phone = request.getParameter("phone");
			String sex = request.getParameter("sex");
			String addr = request.getParameter("addr");
			
			User user = new User();
			user.setAddr(addr);
			user.setCreatetime(new Date());
			user.setPassword(password);
			user.setPhone(phone);
			user.setRealname(realname);
			user.setSex(sex);
			user.setUsername(username);
			user.setId(UUIDUtils.generate());
			this.userService.save(user);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	/**
	 * 用户编辑
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String,Object>();
		
		String id = request.getParameter("id");
		User user = this.userService.findById(id);
		model.put("user", user);
		
		return new ModelAndView("/jsp/user/edit.jsp", model);
	}
	
	
	/**
	 * 更新
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object update(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String id = request.getParameter("id");
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String realname = request.getParameter("realname");
			String phone = request.getParameter("phone");
			String sex = request.getParameter("sex");
			String addr = request.getParameter("addr");
			
			User user = this.userService.findById(id);
			user.setAddr(addr);
			//user.setCreatetime(new Date());
			user.setUpdatetime(new Date());
			user.setPassword(password);
			user.setPhone(phone);
			user.setRealname(realname);
			user.setSex(sex);
			user.setUsername(username);
			this.userService.update(user);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}

	
	/**
	 * 删除
	 * @return
	 */
	@RequestMapping("/del")
	@ResponseBody
	public Object del(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String id = request.getParameter("id");
			this.userService.delete(id);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}
}
